package c_leetcode.a_3_longest_substring;

import java.util.HashSet;
import java.util.Set;

/**
 * 滑动窗口
 * 时间复杂度    O(n)
 * 空间复杂度    O(n)
 *
 * @author FRSF
 * @since 2025/3/4 21:22
 */
public class SlidingWindow {
    public static void main(String[] args) {
        System.out.println(new SlidingWindow().lengthOfLongestSubstring("pwewkaw"));
    }

    public int lengthOfLongestSubstring(String s) {
        Set<Character> occ = new HashSet<>();
        int left = 0, ans = 0;
        for (int i = 0; i < s.length(); i++) {
            while (occ.contains(s.charAt(i))) {
                occ.remove(s.charAt(left++));
            }
            occ.add(s.charAt(i));
            ans = Math.max(ans, i - left + 1);
        }
        return ans;
    }
}
